package com.refTest;

import com.Utils.BaseUtils;
import com.Utils.RefUtils;
import com.refTest.extendTest.Student;

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 映射测试
 */
public class MainReflect {

    private static void alreadyTest() {
        basicTest();
        BaseUtils.printSpaceLines();
    }

    public static void main(String[] args) {
        alreadyTest();

        // 最新测试
        extendTest();
    }

    /**
     * 继承测试
     */
    private static void extendTest() {
        BaseUtils.printCutLine("输出变量名");
        String[] variableNames = RefUtils.getVariableNames(Student.class);
        if (variableNames != null) {
            for (String verName : variableNames) {
                System.out.println(verName);
            }
        }
        try {
            Object obj = Student.class.newInstance();

        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 映射的基础测试 但是有 bug
     */
    private static void basicTest() {
        try {
            Class<?> cls = Class.forName("com.refTest.MyClass");
            Field[] fields = cls.getDeclaredFields();
            Object obj = cls.newInstance();
            for (Field f : fields) {

                // Method 1
                f.setAccessible(true);
                f.set(obj, "lll");

                // Method 2
                String fName = f.getName();
                String methodName = "set" + (fName.charAt(0) + "").toUpperCase() + fName.substring(1);
                System.out.println(methodName);
                Method[] methods = cls.getMethods();
                for (Method m : methods) {
                    if (m.getName().equals(methodName)) m.invoke(obj, "fast");
                }
            }
            printVariableName(cls);
            printMethodName(cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 此方法有bug
     *
     * @param cls 类
     */
    private static void printMethodName(Class<?> cls) {
        BaseUtils.printCutLine("输出MyClass的方法名以及调用该方法");
        System.out.println(cls);
        // Method[] methods = cls.getDeclaredMethods();
        // 无参构造函数的实例对象
        // Object obj = cls.newInstance();

        // 有参构造函数的实例对象
        // Constructor<?>[] constructors = cls.getConstructors();
        // - Object obj = constructors[0].newInstance("__Fy", "男");
        //
        // - for (Method method : methods) {
        // - System.out.println("方法名：" + method.getName());
        // - if (method.getName().contains("printSex")) {
        // - Parameter[] ps = method.getParameters();
        // - for (Parameter p : ps) {
        // - System.out.println("形参类型:" + p.getType());
        // - System.out.println("形参变量:" + p.getName());// 获取不到形参的名称
        // - }
        // - method.invoke(obj, "");
        // - } else method.invoke(obj);
        // - System.out.println("\n");
        // - }
        // - System.out.println("\n");
    }

    private static void printVariableName(Class<?> aClass) {
        Field[] fields = aClass.getDeclaredFields();// 成员变量
        BaseUtils.printCutLine("输出MyClass的变量名");
        for (Field field : fields) {
            System.out.println(field.getName());
        }
    }
}
